using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class LoaiPhongRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //


        public LoaiPhongDTO LayTheoTen(string TenLoaiPhong)
        {
            LoaiPhongDTO objDTO = new LoaiPhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from LoaiPhong in context.LoaiPhongList
                            where LoaiPhong.TenLoaiPhong == TenLoaiPhong
                            select LoaiPhong;
                LoaiPhong objEF = query.First();
                objDTO = CreateLoaiPhongDTOFromLoaiPhongEF(objEF);
            }
            catch (System.Exception)
            {
                objDTO = null;
            }
            return objDTO;
        }
        public bool KiemTraTenTrung(int MaLoaiPhong, string TenLoaiPhong)
        {
            LoaiPhongDTO lp = this.LayTheoTen(TenLoaiPhong);
            if (lp == null)
                return false;
            if (lp.MaLoaiPhong == MaLoaiPhong)
                return false;
            return true;
        }

		//////////////////////////////////////////////////////////

        public static LoaiPhongDTO CreateLoaiPhongDTOFromLoaiPhongEF(LoaiPhong objEf) // EF -> DTO
        {
            LoaiPhongDTO objDTO = new LoaiPhongDTO();
            Mapper.CreateMap<LoaiPhong, LoaiPhongDTO>();
            objDTO = Mapper.Map<LoaiPhong, LoaiPhongDTO>(objEf);
            return objDTO;
        }

        public static LoaiPhong CreateLoaiPhongEFFromLoaiPhongDTO(LoaiPhongDTO objDTO)    //DTO -> EF
        {
            LoaiPhong objEF = new LoaiPhong();
            Mapper.CreateMap<LoaiPhongDTO, LoaiPhong>();
            objEF = Mapper.Map<LoaiPhongDTO, LoaiPhong>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.LoaiPhongList
                   select x.MaLoaiPhong).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.LoaiPhongList
                        orderby x.MaLoaiPhong descending
                        select x.MaLoaiPhong;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(LoaiPhongDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                LoaiPhong objEF = CreateLoaiPhongEFFromLoaiPhongDTO(objDTO);
                context.AddToLoaiPhongList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(LoaiPhongDTO objDTO)
        {
            try
            {
                LoaiPhong objEF = CreateLoaiPhongEFFromLoaiPhongDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.LoaiPhongList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<LoaiPhongDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.LoaiPhongList
                        orderby x.MaLoaiPhong
                        select x).Skip(Start - 1).Take(Record);
            List<LoaiPhongDTO> lst = new List<LoaiPhongDTO>();
            foreach (var obj in query)
            {
                LoaiPhongDTO lp = LoaiPhongRepository.CreateLoaiPhongDTOFromLoaiPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<LoaiPhongDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<LoaiPhongDTO> lst = new List<LoaiPhongDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from LoaiPhong in context.LoaiPhongList
                        select LoaiPhong;
            foreach (var objEf in query)
            {
                LoaiPhongDTO objDTO = CreateLoaiPhongDTOFromLoaiPhongEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public LoaiPhongDTO LayTheoMa(int MaLoaiPhong)
        {
            LoaiPhongDTO objDTO = new LoaiPhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from LoaiPhong in context.LoaiPhongList
                            where LoaiPhong.MaLoaiPhong == MaLoaiPhong
                            select LoaiPhong;
                LoaiPhong objEF = query.First();
                objDTO = CreateLoaiPhongDTOFromLoaiPhongEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }
        public bool Xoa(int MaLoaiPhong, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       ChiTietDoanhThuTheoLoaiPhongRepository ChiTietDoanhThuTheoLoaiPhongRepo = new ChiTietDoanhThuTheoLoaiPhongRepository();
                       ChiTietDoanhThuTheoLoaiPhongRepo.XoaTheoMaLoaiPhong(MaLoaiPhong, XoaKhoaNgoai);

                       PhongRepository PhongRepo = new PhongRepository();
                       PhongRepo.XoaTheoMaLoaiPhong(MaLoaiPhong, XoaKhoaNgoai);

                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.LoaiPhongList
                            where x.MaLoaiPhong == MaLoaiPhong 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

